package com.second.controller.index;

import com.second.model.pojo.FUser;
import com.second.moudle.Result;
import com.second.service.IFCollectionService;
import com.second.service.IFHistoryService;
import com.second.service.IFUserService;
import org.apache.mahout.cf.taste.common.TasteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IFUserService userService;

    @Autowired
    private IFCollectionService collectionService;

    @Autowired
    private IFHistoryService historyService;


    @GetMapping()
    public String index() {
        return "jsp/index";
    }

    /**
     * 默认推荐
     * @return
     * @throws IOException
     * @throws TasteException
     */
    @PostMapping("recommend")
    @ResponseBody
    public Result recommendShop() throws IOException, TasteException {
        HttpSession session = request.getSession();
        if (null != session.getAttribute("user")) {
            FUser user = (FUser) session.getAttribute("user");
            return new Result("200", "查询推荐成功", userService.recommendList(user.getId()));
        } else
            return new Result("200", "查询默认五个店铺", userService.recommend());
    }

    @PostMapping("history/{userId}")
    @ResponseBody
    public Result indexHistory(@PathVariable(name = "userId")Integer userId){
        if (null!=userId){
            return new Result("200","查询成功",historyService.findHistoryThree(userId));
        }else return new Result("400","用户未登录");

    }

    /**
     * 午餐推荐
     * @return
     * @throws IOException
     * @throws TasteException
     */
    @PostMapping("recommendLunch")
    @ResponseBody
    public Result recommendLunchShop() throws IOException, TasteException {
        HttpSession session = request.getSession();
        if (null != session.getAttribute("user")) {
            FUser user = (FUser) session.getAttribute("user");
            return new Result("200", "查询推荐成功", userService.recommendLunchList(user.getId()));
        } else
            return new Result("200", "查询默认五个店铺", userService.recommendLunch());
    }

    /**
     * 午餐推荐
     * @return
     * @throws IOException
     * @throws TasteException
     */
    @PostMapping("recommendDinner")
    @ResponseBody
    public Result recommendDinnerShop() throws IOException, TasteException {
        HttpSession session = request.getSession();
        if (null != session.getAttribute("user")) {
            FUser user = (FUser) session.getAttribute("user");
            return new Result("200", "查询推荐成功", userService.recommendDinnerList(user.getId()));
        } else
            return new Result("200", "查询默认五个店铺", userService.recommendDinner());
    }

    /**
     * 取消收藏
     * @param shopId
     * @param userId
     */
    @PostMapping("cancelRecommend/{shopId}/{userId}")
    @ResponseBody
    public void cancelCollection(@PathVariable(name = "shopId") Integer shopId, @PathVariable(name = "userId") Integer userId){
        collectionService.cancelCollection(shopId,userId);
    }

    /**
     * 添加收藏
     * @param shopId
     * @param userId
     */
    @PostMapping("addRecommend/{shopId}/{userId}")
    @ResponseBody
    public void addCollection(@PathVariable(name = "shopId") Integer shopId, @PathVariable(name = "userId") Integer userId){
        collectionService.addCollection(shopId,userId);
    }
}